package sg.edu.nus.iss.pacman.net.bluetooth.client;

import java.util.Vector;

import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

/**
 * This allows an application to receive device and service discovery event 
 * notification.
 * 
 * @author Daniel
 *
 */
public class ClientDiscoveryListener implements DiscoveryListener {
	
	public static final int STATUS_INITIAL = 0;
	public static final int STATUS_DEVICE_DISCOVERED = 1;
	public static final int STATUS_INQUIRY_COMPLETED = 2;
	public static final int STATUS_SERVICES_DISCOVERED = 3;
	public static final int STATUS_SERVICES_SEARCH_COMPLETED = 4;
	
	/**
	 * List of devices discovered during inquiry
	 */
	private Vector remoteDevices;
	
	/**
	 * Status of discovery event
	 */
	private int status;
	
	public ClientDiscoveryListener(Vector remoteDevices) {
		this.remoteDevices = remoteDevices;
		status = STATUS_INITIAL;
	}

	/**
	 * This method is invoked upon discovery of a device during inquiry.
	 * @param rmtDevice Device discovered during inquiry
	 * @param devClass Service classes, major and minor device class of the 
	 * remote device
	 */
	public void deviceDiscovered(RemoteDevice rmtDevice, DeviceClass devClass) {
		status = STATUS_DEVICE_DISCOVERED;
		remoteDevices.addElement(rmtDevice);
	}

	/**
	 * This method is invoked upon completion of inquiry
	 * @param discType the result of inquiry. The value can be INQUIRY_COMPLETED,
	 * INQUIRY_TERMINATED or INQUIRY_ERROR
	 */
	public void inquiryCompleted(int discType) {
		status = STATUS_INQUIRY_COMPLETED;
	}

	/**
	 * this method is called when a service search is completed or was terminated 
	 * due to an error. 
	 * @param transID Transaction ID identifying the request that initiated the 
	 * service search 
	 * @param respCode Response indicating transaction status
	 */	
	public void serviceSearchCompleted(int transID, int respCode) {
		status = STATUS_SERVICES_SEARCH_COMPLETED;
	}

	/**
	 * This method is invoked when service(s) are found during a service search
	 * @param transID Transaction ID of the service search that posted the result
	 * @param serviceRecord List of services found during the  search request
	 */
	public void servicesDiscovered(int transID, ServiceRecord[] serviceRecord) {
		status = STATUS_SERVICES_DISCOVERED;
	}
	
	public int getStatus() {
		return status;
	}

}
